local GameOver = class("GameOver", function()
	local layer = display.newSprite("bg_blank.png");
	layer:pos(display.cx,display.cy);
	layer:setContentSize(cc.size(display.width,display.height));
	layer:setTouchEnabled(true);
	layer:setVisible(false);
	return layer;
end);

GameOver.delegate = {
	onReplay = "onReplay";	
	onPlayNext = "onPlayNext";
	onReturn = "onReturn";
};

function GameOver:ctor()
	self:initUI();
end

function GameOver:setDelegate(delegate)
	self.m_delegate = delegate;
end 

function GameOver:executeDelegate(func,...)
	print("executeDelegate(func, ...)")
	if self.m_delegate and self.m_delegate[func] then 
		self.m_delegate[func](self.m_delegate,...);
	end 
end 

function GameOver:initUI()
	local replayBtn = cc.ui.UIPushButton.new("btn_green.png",{scale9 = false})
		:onButtonClicked(function()
			self:setVisible(false);
			self:executeDelegate(GameOver.delegate.onReplay);
		end)
		:align(display.CENTER,display.cx, display.cy-100)
		:addTo(self);

		display.newSprite("replay_name.png")
		:pos(0,0)
		:addTo(replayBtn);

	self.m_nextBtn = cc.ui.UIPushButton.new("btn_green.png",{scale9 = false})
		:onButtonClicked(function()
			self:setVisible(false);
			self:executeDelegate(GameOver.delegate.onPlayNext);
		end)
		:align(display.CENTER,display.cx, display.cy)
		:addTo(self);
		display.newSprite("next_name.png")
		:pos(0,0)
		:addTo(self.m_nextBtn);

	cc.ui.UIPushButton.new("buttons_quit.png",{scale9 = false})
		:onButtonClicked(function()
			self:setVisible(false);
			self:executeDelegate(GameOver.delegate.onReturn);
		end)
		:align(display.TOP_LEFT,display.left+20, display.top-20)
		:addTo(self)

	local param = {
			UILabelType = 2, 
			text = "", 
			size = 36,
			align = cc.ui.TEXT_ALIGN_CENTER,
	        color = cc.c3b(255,255,0),
		};
	self.m_tips = cc.ui.UILabel.new(param)
    :align(display.CENTER,display.cx, display.cy+100)
    :addTo(self);
end

function GameOver:show(isWin)
	self.m_nextBtn:setVisible(isWin);
	local str = isWin and "恭喜你闯关成功" or "很遗憾闯关失败";
	self.m_tips:setString(str);
	self:setVisible(true);
end

function GameOver:hide()
	self:setVisible(false);
end

return GameOver;